package cz.proks.easyhelpdesk.gwt.ui.cell;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;

public class CellWithRights<C> extends AbstractCell<C>{
    private final Map<Long, Boolean> rightMap;
    private final Cell<C> delegatedPositiveRightCell;
    private final Cell<C> delegatedNegativeRightCell;
    
    
    public CellWithRights(Map<Long, Boolean> rightMap, Cell<C> delegatedPositiveRightCell, Cell<C> delegatedNegativeRightCell) {
        super(getJoinedConsumedEvents(delegatedPositiveRightCell, delegatedNegativeRightCell));
        this.rightMap = rightMap == null ? new HashMap<Long, Boolean>() : rightMap;
        this.delegatedPositiveRightCell = delegatedPositiveRightCell;
        this.delegatedNegativeRightCell = delegatedNegativeRightCell;
    }

    @Override
    public void onBrowserEvent(com.google.gwt.cell.client.Cell.Context context, Element parent, C value, NativeEvent event, ValueUpdater<C> valueUpdater) {
        Long rowKey = Long.valueOf(context.getKey().toString());
        
        if (rightMap.containsKey(rowKey) && rightMap.get(rowKey) && 
            delegatedPositiveRightCell != null &&
            delegatedPositiveRightCell.getConsumedEvents() != null &&
            delegatedPositiveRightCell.getConsumedEvents().contains(event.getType()))
            delegatedPositiveRightCell.onBrowserEvent(context, parent, value, event, valueUpdater);
        
        else if ((!rightMap.containsKey(rowKey) || !rightMap.get(rowKey)) && 
                 delegatedNegativeRightCell != null &&
                 delegatedNegativeRightCell.getConsumedEvents() != null &&
                 delegatedNegativeRightCell.getConsumedEvents().contains(event.getType()))
            delegatedNegativeRightCell.onBrowserEvent(context, parent, value, event, valueUpdater);
        
        else
            ; // NOP
    }

    @Override
    public void render(com.google.gwt.cell.client.Cell.Context context, C value, SafeHtmlBuilder sb) {
        Long rowKey = Long.valueOf(context.getKey().toString());
        
        if (rightMap.containsKey(rowKey) && rightMap.get(rowKey) && 
            delegatedPositiveRightCell != null)
            delegatedPositiveRightCell.render(context, value, sb);
        
        else if ((!rightMap.containsKey(rowKey) || !rightMap.get(rowKey)) && 
                delegatedNegativeRightCell != null)
            delegatedNegativeRightCell.render(context, value, sb);
        
        else
            ; // NOP        
    }    
    
    private static Set<String> getJoinedConsumedEvents(Cell<?> cell1, Cell<?> cell2) {
        Set<String> consumedEvents = new HashSet<String>();
        
        if (cell1 != null && cell1.getConsumedEvents() != null)
            consumedEvents.addAll(cell1.getConsumedEvents());
        
        if (cell2 != null && cell2.getConsumedEvents() != null)
            consumedEvents.addAll(cell2.getConsumedEvents());
        
        return consumedEvents;
    }
}
